<?php

namespace app\api\controller\product;

use think\Controller;
use think\Request;

class Product extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->TypeModel = model('Product.Type');
        $this->ProductModel = model('Product.Product');
        $this->UserModel = model('User.User');
        $this->VipModel = model('Product.Vip');
    }

    //首页数据
    public function index()
    {
        //查询推荐商品
        $Recommend = $this->ProductModel->with(['type'])->where(['flag' => 3])->order('id', 'desc')->limit(5)->select();

        //查询新品商品
        $News = $this->ProductModel->with(['type'])->where(['flag' => 1])->order('id', 'desc')->limit(4)->select();

        //查询热销商品
        $Hot = $this->ProductModel->with(['type'])->where(['flag' => 2])->order('id', 'desc')->limit(6)->select();

        //查询分类
        $Type = $this->TypeModel->select();

        //封装数组
        $data = [
            'Recommend' => $Recommend, //推荐
            'News' => $News, //新品
            'Hot' => $Hot, //热销
            'Type' => $Type, //商品分类
        ];

        $this->success('返回首页数据', null, $data);
        exit;
    }

    //商品列表
    public function type()
    {
        $typeid = $this->request->param('typeid', 0);
        $flag = $this->request->param('flag', 0);
        $orderby = $this->request->param('orderby', 'id');
        $keywords = $this->request->param('keywords', '');
        $where = [];
        $limit = 10; //分页查询10条

        $type = $this->TypeModel->find($typeid);

        //有分类id就查询分类id,没有就查询全部
        if ($type) {
            $where['typeid'] = $typeid;
        }

        //判断是否有选择属性
        if ($flag) {
            $where['flag'] = $flag;
        }

        //模糊查询
        if (!empty($keywords)) {
            $where['name'] = ['like', "%$keywords%"];
        }

        //获取商品列表
        $prolist = $this->ProductModel->where($where)->order($orderby, 'desc')->select();
        $count = $this->ProductModel->count();
        //查询分类
        $typelist = $this->TypeModel->select();

        $data = [
            'product' => $prolist,
            'typelist' => $typelist,
            'count' => $count,
        ];

        $this->success('查询商品数据成功', null, $data);
        exit;
    }

    //商品详情
    public function info()
    {
        if ($this->request->isPost()) {

            //获取商品id
            $proid = $this->request->param('proid', 0);
            $userid = $this->request->param('userid', 0);

            //查询商品的详细
            $product = $this->ProductModel->with(['type'])->find($proid);

            if (!$product) {
                $this->error('商品不存在');
                exit;
            }

            $user = $this->UserModel->find($userid);
            $vip = isset($user) ? $user['vip'] : 0;

            if ($vip > 0) {
                $level = $this->VipModel->where(['proid' => $proid, 'level' => $vip])->value('price');
                $product['level'] = $level;
            }

            $this->success('返回商品详情', null, $product);
            exit;
        }
    }


    //分页查询
    public function limitProduct()
    {
        if ($this->request->isPost()) {
            $typeid = $this->request->param('typeid', 0);
            $orderby = $this->request->param('orderby', 'id'); //排序
            $page = $this->request->param('page', 0); //请求第几页
            $pageSize = $this->request->param('pageSize', 10); //每页请求的数量
            $where = [];
            if ($typeid) {
                $where['typeid'] = $typeid;
            }

            $proList = $this->ProductModel->where($where)->order($orderby, 'desc')->limit(($page * $pageSize), $pageSize)->select();
            $count = $this->ProductModel->where($where)->count();

            $data = [
                'proList' => $proList,
                'count' => $count,
            ];
            if ($proList === FALSE) {
                $this->error('返回商品数据失败');
                exit;
            } else {
                $this->success('返回商品数据成功', null, $data);
                exit;
            }
        }
    }

    //查询所有商品分类
    public function productType()
    {
        if ($this->request->isPost()) {

            $type = $this->TypeModel->select();

            if ($type === FALSE) {
                $this->error('暂无商品分类');
                exit;
            } else {
                $this->success('返回商品分类成功', null, $type);
                exit;
            }
        }
    }
}
